<?php

namespace app\http\middleware;

use think\facade\Cache;

class ApiGetUserid
{
    public function handle($request, \Closure $next)
    {
        // 获取头部
        $param = request()->header();
        if (array_key_exists('token', $param)) {
            $token = $param['token'];
            $user = Cache::get($token);
            if ($user) {
                $request->userToken = $token;
                // 判断第三方登录还是其他登陆
                $request->userId = array_key_exists('user_id', $user) ? $user['user_id'] : $user['id'];
                $request->userTokenUserInfo = $user;
            }
        }
        return $next($request);
    }
}
